package com.dycong.common.leetcode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-09-05
 * Time: 8:56
 */
public class LinkListSum {

    public LinkNode addTwoNumbers(LinkNode l1, LinkNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        LinkNode tmp = new LinkNode(0);
        LinkNode head = tmp;
        int capicty = 0;
        while (l1 != null || l2 != null || capicty > 0) {
            int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + capicty;
            tmp.next = new LinkNode(sum % 10);
            capicty = sum / 10;
            l1 = l1 == null ? l1 : l1.next;
            l2 = l2 == null ? l2 : l2.next;
        }

        return head.next;

    }

    public class LinkNode {
        int val;
        LinkNode next;

        public LinkNode(int val) {
            this.val = val;
        }
    }
}
